/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Services;

import Entitys.SoundTrack;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

/**
 *
 * @author Administrador
 */
public class SoundTrackDAO {
    private static EntityManagerFactory emf= Persistence.createEntityManagerFactory("GRLPU");
    private static EntityManager em=emf.createEntityManager();
    
    public void create(SoundTrack sont){
        em.getTransaction().begin();
        try {
            em.persist(sont);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
    
    public boolean delete(SoundTrack sont) {

        
        em.getTransaction().begin();
        SoundTrack st= em.find(SoundTrack.class, sont.getId());
        boolean ret = false;
        try {
            em.remove(st);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }
    
    public List<SoundTrack> ReadAll(){
        TypedQuery<SoundTrack> q=em.createQuery("SELECT s FROM SoundTrack s",SoundTrack.class);
        try {
          return q.getResultList();  
        } catch (Exception e) {
        e.printStackTrace();
        } finally {
         em.close();   
        }
        return null;
    }
    
    public SoundTrack find(int id){
        TypedQuery<SoundTrack> q=em.createQuery("SELECT s FROM SoundTrack s WHERE s.id LIKE :id ", SoundTrack.class);
        try {
          return q.getSingleResult();  
        } catch (Exception e) {
         e.printStackTrace();    
        } finally {
         em.close();   
        }
        return null;
    }
}